package com.linsir.lin.service.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linsir.lin.dto.GridDTO;
import com.linsir.lin.dto.OrganizationDTO;
import com.linsir.lin.mapper.OrganizationMapper;
import com.linsir.lin.pojo.Organization;
import com.linsir.lin.service.OrganizationService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName:linsir-lin
 * @Package:com.linsir.lin.service
 * @Title: OrganizationServiceImpl
 * @Description: 组织接口实现
 * @Create:2021/6/28 23:04 by @author:Linsir
 * @Update:2021/6/28 23:04 by @author:Linsir
 * @UpdateRemark: 初始创建
 * @Version: 1.0.0
 */

@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements IService<Organization> , OrganizationService {


    @Override
    public GridDTO<Organization> grid() {
        List<Organization> list = list();
        Integer size = list.size();
        return new GridDTO<Organization>("0","成功",size.longValue(),list);
    }

    @Override
    public List<OrganizationDTO> organList() {
        List<OrganizationDTO> organizationDTOList = new ArrayList<OrganizationDTO>();
        List<Organization> organizations = list();

        organizations.forEach(o->{
            try {
                OrganizationDTO organizationDTO = toDTO(o);
                organizationDTOList.add(organizationDTO);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });
        return toTree(organizationDTOList,0);
    }

    @Override
    public OrganizationDTO toDTO(Organization organization) throws InvocationTargetException, IllegalAccessException {
        OrganizationDTO organizationDTO = new OrganizationDTO();
        BeanUtils.copyProperties(organizationDTO,organization);
        organizationDTO.setTitle(organization.getName());
        organizationDTO.setSpread(true);
        return organizationDTO;
    }

    @Override
    public List<OrganizationDTO> toTree(List<OrganizationDTO> treeList, Integer pid) {
        List<OrganizationDTO> retList = new ArrayList<OrganizationDTO>();
        treeList.forEach(o->{
            if (pid.equals(o.getPid()))
            {
                retList.add(findChildren(o, treeList));
            }
        });
        return retList;
    }

    @Override
    public OrganizationDTO findChildren(OrganizationDTO parent, List<OrganizationDTO> treeList) {

        treeList.forEach(o->{
            if (parent.getId().equals(o.getPid()))
            {
                if (parent.getChildren()==null)
                {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(findChildren(o,treeList));
            }
        });

        return  parent;

    }

    @Override
    public List<Organization> getOrganizationsByIds(List<Integer> ids) {
        return listByIds(ids);
    }

    @Override
    public List<OrganizationDTO> getChildren( List<OrganizationDTO> list) {
        List<OrganizationDTO> result = new ArrayList<OrganizationDTO>();

        list.forEach( organizationDTO ->
        {
            result.add(organizationDTO);

            List<OrganizationDTO> children = organizationDTO.getChildren();
            if (children!=null&&children.size()>0)
            {
                List<OrganizationDTO> organizationDTOList = this.getChildren(children);
                result.addAll(organizationDTOList);
            }

        });

        if (result.size() > 0) {
            for (OrganizationDTO organizationDTO : result) {
                organizationDTO.setChildren(null);
            }
        }
        return  result;
    }

    @Override
    public List<OrganizationDTO> getTree(OrganizationDTO organizationDTO) {

        List<OrganizationDTO> organizationDTOList = new ArrayList<OrganizationDTO>();
        organizationDTOList.add(organizationDTO);

        List<OrganizationDTO> ch=organizationDTO.getChildren();

        if (organizationDTO.getChildren()!=null&&organizationDTO.getChildren().size()>0)
        {
            organizationDTOList.addAll(getChildren(organizationDTO.getChildren()));
        }
        return organizationDTOList;
    }

    @Override
    public OrganizationDTO getByOrganId(Integer organId) throws InvocationTargetException, IllegalAccessException {
        return toDTO(getBaseMapper().selectById(organId));
    }

    @Override
    public Organization getById(Integer id){
        return baseMapper.selectById(id);
    }
}
